//
//  RegisterViewController.swift
//  jiaoyi
//
//  Created by wjk on 2018/8/8.
//  Copyright © 2018年 wjk. All rights reserved.
//

import UIKit

class RegisterViewController: UIViewController {

    @IBOutlet weak var btnCode: UIButton!
    @IBOutlet weak var tfMobile: UITextField!
    @IBOutlet weak var tfCode: UITextField!
    @IBOutlet weak var tfPass: UITextField!
    @IBOutlet weak var tfPassVer: UITextField!
    
    @IBOutlet weak var btnImgCode: UIButton!
    @IBOutlet weak var btnReg: UIButton!
    
    @IBOutlet weak var btnOpen: UIButton!
    
    @IBOutlet weak var btnDele: UIButton!
    var isReg: Bool! = false;
    
    @IBOutlet weak var tfImgCode: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        if (!isReg) {
            btnReg?.setTitle("忘记密码", for: UIControlState.normal)
            
            btnOpen?.isHidden = true;
            btnDele?.isHidden = true
            self.title = "忘记密码";
        }else {
            self.title = "立即注册";
        }
        loadCap()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func selectE(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected;
    }
    @IBAction func openUrl(_ sender: UIButton) {
        self.navigationController?.pushViewController(WebViewController.showWebView(["http://1"], "注册协议"), animated: true)
    }
    
    func loadCap() -> Void {
        NetWorkRequest(.cap()) { (res) -> (Void) in
            self.btnImgCode.setBackgroundImage((res["image"] as! UIImage), for: .normal)
        }
    }
    
    @IBAction func getImgCode(_ sender: UIButton) {
        loadCap()
    }
    
    @IBAction func getCode(_ sender: UIButton) {
        
        if tfImgCode.text!.isEmpty {
            showMessage(message: "请先输入图片验证码")
            return
        }
        
        showHUD()
        if isReg==true {
            NetWorkRequest(.code(user_phone: tfMobile.text!, type: "1", captcha: tfImgCode.text!)) { (res) -> (Void) in
                if res["code"]! as! String == "000000" {
                    sender.startTimer(title: "s")
                }else {
                    self.tfImgCode.text = ""
                    self.loadCap()
                }
                dismissHUD()
            }
        }else {
            NetWorkRequest(.code(user_phone: tfMobile.text!, type: "2", captcha: tfImgCode.text!)) { (res) -> (Void) in
                if res["code"]! as! String == "000000" {
                    sender.startTimer(title: "s")
                }else {
                    self.tfImgCode.text = ""
                    self.loadCap()
                }
                dismissHUD()
            }
        }
    }
    
    

    @IBAction func click(_ sender: UIButton) {
        self.view.endEditing(true)
        
        if self.btnOpen.isSelected == false {
            showMessage(message: "请先同意用户协议")
            return
        }
        
        if tfImgCode.text!.isEmpty {
            showMessage(message: "请先输入图片验证码")
            return
        }
        
        if tfCode.text!.isEmpty {
            showMessage(message: "请先输入手机验证码")
            return
        }
        if tfPass.text!.isEmpty {
            showMessage(message: "请先输入密码")
            return;
        }
        if tfPass.text! != tfPassVer.text! {
            showMessage(message: "两次密码不一致")
            return
        }
        showHUD()
        if isReg == true {
            NetWorkRequest(.register(param: ["source_type":"1","app_id":AppConstants.app_id,"app_secret":AppConstants.app_secret,"user_phone":tfMobile.text!,"password":tfPass.text!,"password_confirmation":tfPassVer.text!,"captcha":tfCode.text!])) { (res) -> (Void) in
                if res["code"] as? String == "000000" {
                    let data = res["data"] as! [String:Any]
                    let access_token = data["access_token"] as! String//真实的token
                    UserDefaults.User.set(access_token, forKey: .authorization)
                    UserDefaults.User.set(true, forKey: .isLogin)
                    DispatchQueue.main.async {
                        NotificationCenter.default.post(name: NSNotification.Name.UserDidLogin, object: nil)
                    }
                    (UIApplication.shared.delegate as! AppDelegate).user_name = self.tfMobile.text!
                    
                    UserDefaults.standard.set(self.tfMobile.text!, forKey: "user_name")
//                    self.navigationController?.popToRootViewController(animated: true)
                    UIApplication.shared.delegate?.window?!.rootViewController = R.storyboard.main().instantiateInitialViewController()
                }
            }
        }else {
            NetWorkRequest(.forgotPwd(param: ["user_phone":tfMobile.text!,"password":tfPass.text!,"password_confirmation":tfPassVer.text!,"captcha":tfCode.text!])) { (res) -> (Void) in
                if res["code"] as! String == "000000" {
                    showMessage(message: "找回密码成功，请登录")
                    self.navigationController?.popViewController(animated: true)
                }
            }
        }
        
    }
    
    deinit {
        
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
